package net.hancl.modules.sys.shiro;

import net.hancl.common.utils.spring.SysApplicationContext;
import net.hancl.modules.sys.constants.SysComm;
import net.hancl.modules.sys.service.IUserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SysUserFilter extends PathMatchingFilter {

    @Autowired
    private IUserService userService;

    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {

        String username = (String)SecurityUtils.getSubject().getPrincipal();
        if (userService == null) {
            userService = (IUserService) SysApplicationContext
                    .getBean("userService");
        }
        request.setAttribute(SysComm.SYS_USER, userService.getUser(username));
        return true;
    }


}